using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;
namespace DAL
{
    public class tbMaSoSPDAL
    {
        private SqlConnection cn = null;
        public tbMaSoSPDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbMaSoSP MS)
        {
            SqlCommand cm = new SqlCommand("sp_MaSoSP_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdMaSoSP", MS.IdMaSoSP);
            cm.Parameters.AddWithValue("@IdCTDonHang", MS.IdCTDonHang);
            cm.Parameters.AddWithValue("@MaSo", MS.MaSo);
            cm.Parameters.AddWithValue("@TrangThai", MS.TrangThai);  
            cm.ExecuteNonQuery();

        }
        public void Update(tbMaSoSP  MS)
        {
            SqlCommand cm = new SqlCommand("sp_MaSoSP_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdMaSoSP", MS.IdMaSoSP);
            cm.Parameters.AddWithValue("@IdCTDonHang", MS.IdCTDonHang);
            cm.Parameters.AddWithValue("@MaSo", MS.MaSo);
            cm.Parameters.AddWithValue("@TrangThai", MS.TrangThai);  
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_MaSoSP_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdMaSoSP", Id);
            cm.ExecuteNonQuery();
        }
        public tbMaSoSPCollection  Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_MaSoSP_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbMaSoSPCollection  mscollection = new tbMaSoSPCollection ();
            while (dr.Read())
            {
               tbMaSoSP MS = new tbMaSoSP ();
               MS.IdMaSoSP  = int.Parse(dr.GetValue(0).ToString());
               MS.IdCTDonHang  = int.Parse(dr.GetValue(1).ToString());
               MS.MaSo = int.Parse(dr.GetValue(2).ToString());
               MS.TrangThai = bool .Parse (dr.GetValue(3).ToString());
               mscollection.Add(MS);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return mscollection;
        }
    }
}
